#include "btime.h"

using namespace core;

BasicTime::BasicTime() : m_value(0),
    m_unit(1)
{

}

BasicTime::BasicTime(uint value) : m_value(value),
    m_unit(1)
{

}

BasicTime::BasicTime(uint value, uint unit) : m_value(value),
    m_unit(unit)
{

}

BasicTime::~BasicTime()
{

}

uint BasicTime::getTime() const
{
    return m_value;
}

uint BasicTime::getTimeUnit() const
{
    return m_unit;
}

void BasicTime::setTime(uint value)
{
    m_value = value;
}

void BasicTime::setTimeUnit(uint unit)
{
    m_unit = unit;
}

bool BasicTime::add(uint value)
{
    const bool overflow = (m_value + value) > m_value;
    m_value += value;

    return !overflow;
}

bool BasicTime::sub(uint value)
{
    const bool overflow = (m_value - value) < m_value;
    m_value -= value;

    return !overflow;
}

BasicTime& BasicTime::operator++()
{
    if ((m_value + m_unit) > m_value)
    {
        m_value += m_unit;
    }

    return *this;
}

BasicTime BasicTime::operator++(int)
{
    BasicTime ret(m_value, m_unit);
    ++(*this);

    return ret;
}

BasicTime& BasicTime::operator--()
{
    if ((m_value - m_unit) < m_value)
    {
        m_value -= m_unit;
    }

    return *this;
}

BasicTime BasicTime::operator--(int)
{
    BasicTime ret(m_value, m_unit);
    --(*this);

    return ret;
}

bool BasicTime::operator==(const BasicTime& other) const
{
    return m_value == other.m_value;
}

bool BasicTime::operator!=(const BasicTime& other) const
{
    return m_value != other.m_value;
}

bool BasicTime::operator<(const BasicTime& other) const
{
    return m_value < other.m_value;
}

bool BasicTime::operator<=(const BasicTime& other) const
{
    return m_value <= other.m_value;
}

bool BasicTime::operator>(const BasicTime& other) const
{
    return m_value > other.m_value;
}

uint qHash(const core::BasicTime& time)
{
    return time.getTime();
}
